package com.xy.mvvmdemo.fragment


import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import androidx.navigation.Navigation
import androidx.navigation.fragment.NavHostFragment
import com.xy.mvvmdemo.Activity2
import com.xy.mvvmdemo.R
import com.xy.mvvmdemo.bean.LiveDataBus
import kotlinx.android.synthetic.main.public_fragment.*


/**
 * @ClassName FragmentOne
 * @Author xy
 * @Date 2021/7/8 13:53
 * @Description TODO
 */
const val POSITION = "Position"
const val TAG = "Fragment"

class MyFragment : LazyFragment(), View.OnClickListener {

    private var liveData = MutableLiveData<String>().apply { value = "" }

    companion object {
        public fun newIntance(position: Int): MyFragment {
            var bundle = Bundle()
            bundle.putInt(POSITION, position)
            var myFragment = MyFragment()
            myFragment.arguments = bundle
            return myFragment
        }
    }

    override fun initData(view: View, savedInstanceState: Bundle?) {
        liveData.observe(this, {
            text.text = it.toString()
        })
        mainThread.setOnClickListener(this)
        sonThread.setOnClickListener(this)
        send.setOnClickListener {
//            startActivity(Intent(context, Activity2::class.java))
//            NavHostFragment.findNavController(this).navigate(R.id.action_myFragment_to_myFragmentOnePage)
            Navigation.findNavController(it).navigate(R.id.activity2)
//            activity?.let { it1 -> Navigation.findNavController(it1,R.id.main).navigate(R.id.activity2) }
        }

        jump.setOnClickListener {
//            LiveDataBusBeta.getInstance().with("Activity2", String::class.java).value = "来自MainActivity的问候"
            LiveDataBus.getInstance().with("Activity2", String::class.java).value =
                "来自MainActivity的问候"
        }

//        LiveDataBusBeta.getInstance().with("MainActivity", String::class.java).observe(this,{
        LiveDataBus.getInstance().with("MainActivity", String::class.java).observe(this, {
            Toast.makeText(context, it.toString(), Toast.LENGTH_SHORT).show()
        })


    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.mainThread -> {
                liveData.value = "主线程发的消息"
            }

            R.id.sonThread -> {
                var thread = object : Thread() {
                    override fun run() {
                        liveData.postValue("子线程发的消息")
                    }
                }
                thread.start()
            }
        }
    }


    override fun setLayoutRes(): Int {
        return R.layout.public_fragment
    }

    override fun startFragment() {
        Log.i(TAG, "name:fragment${arguments?.getInt(POSITION)} --->startFragment -->展示Fragment内容")


        text.text = when (arguments?.getInt(POSITION)) {
            1 -> {
                "FragmentOne"
            }
            2 -> {
                "FragmentTwo"
            }
            3 -> {
                "FragmentThree"
            }
            4 -> {
                "FragmentFour"
            }
            5 -> {
                "FragmentFive"
            }
            else -> {
                ""
            }
        }
        ll.visibility = View.VISIBLE


}

override fun stopFragment() {
    Log.i(TAG, "name:fragment${arguments?.getInt(POSITION)} --->startFragment -->暂停Fragment内容")
}

override fun setUserVisibleHint(isVisibleToUser: Boolean) {
    var tabIndex = arguments?.getInt(POSITION)
    super.setUserVisibleHint(isVisibleToUser)
    Log.i(TAG, "name:fragment$tabIndex --->setUserVisibleHint$isVisibleToUser")
}



}